<?php

/**
 * This is the model class for table "Rates".
 *
 * The followings are the available columns in table 'Rates':
 * @property string $FromCountry
 * @property string $ToCountry
 * @property string $ToCountryCode
 * @property string $LocalRate
 * @property string $TollFreeRate
 * @property integer $Orders
 */
class Rates extends CActiveRecord
{
	public $ii = NULL;
	public $balance = NULL;
	private static $_list = NULL;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Rates the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Rates';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Orders', 'numerical', 'integerOnly'=>true),
			array('FromCountry, ToCountry, ToCountryCode', 'length', 'max'=>255),
			array('LocalRate, TollFreeRate', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('FromCountry, ToCountry, ToCountryCode, LocalRate, TollFreeRate, Orders', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'FromCountry' => 'From Country',
			'ToCountry' => 'To Country',
			'ToCountryCode' => 'To Country Code',
			'LocalRate' => 'Local Rate',
			'TollFreeRate' => 'Toll Free Rate',
			'Orders' => 'Orders',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('FromCountry',$this->FromCountry,true);
		$criteria->compare('ToCountry',$this->ToCountry,true);
		$criteria->compare('ToCountryCode',$this->ToCountryCode,true);
		$criteria->compare('LocalRate',$this->LocalRate,true);
		$criteria->compare('TollFreeRate',$this->TollFreeRate,true);
		$criteria->compare('Orders',$this->Orders);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
		/**
	 * Lấy tỉ giá rates theo cen (database lưu theo $ -> cen = $*100
	 *
	 * @return returnType returnDescription
	 */
	public static function getInterRate() 
	{
		if(self::$_list === NULL)
		{
			self::$_list = Rates::model()->findAll(array('order'=>'Orders'));
			foreach(self::$_list as $index => $item)
			{
				self::$_list[$index]->LocalRate *= 100;
				self::$_list[$index]->ii = $index;
			}	
		}
	    
	    return self::$_list;
	}
	
	/**
	 * Hàm này được gọi tính số phút trả về.
	 *
	 * @return returnType returnDescription
	 */
	public function getMinute() {
	    
	    $balance =  isset($_REQUEST['balance'])?$_REQUEST['balance']: '5';
	    $balance *= 100;
	    
	    $localRate = $this->LocalRate;
	    return floor($balance/$localRate);
	}
}